# -*- coding: utf-8 -*-
# @Time    : 2020/11/10 14:45
# @Author  : suhong
# @File    : send_wechat.py
# @Software: PyCharm
import json
import traceback

from fastapi import APIRouter
from pydantic import BaseModel
from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.tools.all_requests.mrequest import MRequest
from re_common.baselibrary.utils.core.mlamada import bools_string

from apps.core.m_route import ContextIncludedRoute
from apps.core.return_info import InputInfoModel, ReturnInfo
from apps.crawler_platform.util.requestapihelper import RequestApiHelper

router = APIRouter(route_class=ContextIncludedRoute)


class MsgModel(BaseModel):
    author: str = ""
    msg: str = ""


@router.post("/wechat/send_msg")
async def get_page_link(input: InputInfoModel[MsgModel]):
    """
    发送消息到企业微信
    艾特成员时，名字用分号链接；都为中文。全体成员为 所有人
    {"author:"","msg":""}
    example:
        {"author:"所有人","msg":"123"}
        {"author:"苏鸿","msg":"123"}
        {"author:"苏鸿;徐江","msg":"123"}
    """
    author_dict = {
        "苏鸿": "suhong",
        "徐江": "xujiang",
        "曾垂诺": "cengchuinuo",
        "覃业猛": "tanyemeng",
        "所有人": "@all",
    }
    return_info = ReturnInfo()
    author = input.data.author
    msg = input.data.msg
    try:
        if len(author) == 0:
            data = {
                "msgtype": "text",
                "text": {
                    "content": msg,
                }
            }
        else:
            author_list = list(map(lambda x: author_dict[x], author.split(";")))
            data = {
                "msgtype": "text",
                "text": {
                    "mentioned_list": author_list,
                    "content": msg
                }
            }
        url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1f645513-d7ea-45c2-a812-c1ee61f56aaf'
        headers = {"Content-Type": "application/json"}
        rrq = RequestApiHelper.get_rrq()
        kwargs = {
            "rrq": rrq,
            "header": headers,
            "url": url,
            "timeout": 30,
            "data": json.dumps(data),
            "moths": MRequest.POST,
            "middler_list": [rrq.status_code_middlerwares]
        }
        bools, dicts = await RequestApiHelper.etl_remote_meta(**kwargs)
        # rrq = AioHttpRequest()
        # rrq.set_url(url).set_data(json.dumps(data)).set_timeout(30).set_header(headers).set_middler_list(
        #     [rrq.status_code_middlerwares])
        # bools, dicts = await rrq.run(MRequest.POST)
        if bools:
            return_info.status = bools_string(True)
            return_info.msg = ""
            return_info.msg_code = 200
        else:
            return_info.status = bools_string(False)
            return_info.msg = dicts
            return_info.msg_code = 1000
    except Exception as e:
        return_info.status = bools_string(False)
        return_info.msg = traceback.print_exc()
        return_info.msg_code = 1000

    return_info.data = data
    return return_info.todict()
